import { createPinia } from 'pinia'
import { useUserStore } from '@/stores/user'

const pinia = createPinia()
const userStore = useUserStore(pinia)


function permissionAuth(data,permissionArray) {
  return permissionArray?.includes(data)
}

/**
 * 筛选动态路由
 */

export function hasPermission(data,arr) {  
  let homeMenu = []
  data.forEach((item) => {
    //有permission的路由才做判断，没有的直接放进数组
    if (item.meta.permission && !item.children && permissionAuth(item.meta.permission,arr)) {
      homeMenu.push(item)
    } else if (item.meta.permission && item.children) {      
      let obj = {}
      if (permissionAuth(item.meta.permission,arr)) {        
        obj = {
          ...item,
          children: []
        }
        item.children.forEach((child) => {
          if (permissionAuth(child.meta.permission,arr)) {
            obj.children.push(child)
          }
        })
      }
      // 判断obj是否为空
      if (obj.path) {
        homeMenu.push(obj)
      }
    } else {
      homeMenu.push(item)
    }
  })

  return homeMenu
}

export default permissionAuth
